﻿//----------------------------------------------------------------------------
// Copyright (c) Pavel Tishkin 2012-2013.
//
// You must not remove this notice, or any other, from this software.
//----------------------------------------------------------------------------

namespace Lib.Dxp.Core

open System
open System.Data
open System.Data.SqlClient



type Log() =
    static let mutable _log = NLog.LogManager.GetLogger("LibDxpCore")


    static let _ignoreFileMode              = true
    static let mutable _useStringBuilder    = false
    static let mutable _logFileMode         = Configuration.ConfigurationManager.AppSettings.["logmode"].to_s().ToUpper()
    static let mutable _stringBuffer        = System.Text.StringBuilder()

    static let toSB ([<System.ParamArray>] xs: obj[]) =
        lock _stringBuffer (fun () -> 
                                for x in xs do begin _stringBuffer.Append(x).Append(" ") |> ignore end
                                _stringBuffer.AppendLine() |> ignore
                                )


    static member Log with get() = 
                                _log.Info "I0001 Logger is started"
                                _log //and set value = _log <- value

    static member UseStringBuilder  with get() = _useStringBuilder  and set value = _useStringBuilder <- value
    static member Mode              with get() = _logFileMode       and set value = _logFileMode <- value
    
    static member Clear()           = _stringBuffer <- new System.Text.StringBuilder()
    static member Buffer            = let r = _stringBuffer.ToString() in Log.Clear(); r

    static member Excp(ex : Exception)                      = _log.Error("*** EXCEPTION ***", ex.Message, ex.Source, ex.StackTrace)
    static member ExcpVerbose(ex : Exception)               = _log.Error(sprintf "*** EXCEPTION : %s\r\n%s\r\n%s\r\n%s" (ex.GetType().Name) ex.Message ex.Source ex.StackTrace)
    
    static member Info  ([<System.ParamArray>] xs: obj[])   = if _useStringBuilder then toSB(xs) else _log.Info (xs |> Seq.map to_s |> "\t".join) 
    static member Debug ([<System.ParamArray>] xs: obj[])   = if _useStringBuilder then toSB(xs) else _log.Debug(xs |> Seq.map to_s |> "\t".join)
    static member Error ([<System.ParamArray>] xs: obj[])   = if _useStringBuilder then toSB(xs) else _log.Error(xs |> Seq.map to_s |> "\t".join)
    static member Qry   ([<System.ParamArray>] xs: obj[])   = if _useStringBuilder then toSB(xs) else _log.Trace(xs |> Seq.map to_s |> "\t".join)

